package com.cskaoyan.service;

import com.cskaoyan.bean.BaseParam;

import com.cskaoyan.bean.BaseData;

import com.cskaoyan.bean.user.SearchHistory;
import com.cskaoyan.bean.user.SearchHistoryExample;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * \* Description:
 * \
 */
@Service
public class SearchHistoryServiceImpl implements SearchHistorySevice {

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @SuppressWarnings("all")
    @Override
    public BaseData<SearchHistory> history(BaseParam param, Integer userId, String keyword) {
        PageUtil.start(param);

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId != null && !"".equals(userId))
            criteria.andUserIdEqualTo(userId);
        if (keyword != null && !"".equals(keyword))
            criteria.andKeywordEqualTo(keyword );

        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);

        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        BaseData<SearchHistory> searchHistoryBaseData = new BaseData<>(searchHistoryPageInfo, searchHistories);

        return searchHistoryBaseData;
    }
}
